package com.sgai.dcac.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sgai.dcac.R;
import com.sgai.dcac.contract.LoginContract;
import com.sgai.dcac.presenter.TestLoginPresenter;
import com.sgai.dcac.ui.common.BaseActivity;
import com.sgai.dcac.ui.view.LoginView;

public class LoginActivity extends BaseActivity implements View.OnClickListener,LoginView {

    private EditText usernameEdt,passwordEdt;
    private Button loginBtn;
    private LoginContract.Presenter loginPresenter;
    private Toast toast;

    @Override
    protected void findViewById() {
        usernameEdt = (EditText) findViewById(R.id.username);
        passwordEdt = (EditText) findViewById(R.id.password);
        loginBtn = (Button) findViewById(R.id.login_button);
    }

    @Override
    protected void setUpView() {
//        loginPresenter = new LoginPresenter(this);
        loginPresenter = new TestLoginPresenter(this);
        loginBtn.setOnClickListener(this);
        Log.e("","12");
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_login;
    }

    public static void startThisActivity (Activity activity) {
        activity.startActivity(new Intent(activity,LoginActivity.class));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_button:
                loginPresenter.doLogin(usernameEdt.getText().toString()
                ,passwordEdt.getText().toString());
                break;
        }
    }

    @Override
    public void success(String str) {
        //提示&&跳转
        if (toast == null) {
            toast = Toast.makeText(this,str,Toast.LENGTH_LONG);
            toast.show();
        } else {
            toast.setText(str);
            toast.show();
        }
        MenuActivity.startThisActivity(this);
    }

    @Override
    public void failure(String str) {
        //提示
        if (toast == null) {
            toast = Toast.makeText(this,str,Toast.LENGTH_LONG);
            toast.show();
        } else {
            toast.setText(str);
            toast.show();
        }
    }
}
